﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using DataAccess;
using DataAccess.Entity;

namespace SkynetMongoDBAccess
{
    public class MongoAccess : IDataAccess, IDataStructure
    {
        private readonly MongoKeyValueDB _mongo;
        private bool _isClose = false;
        private readonly ConvertSql _convert = new ConvertSql();
        public MongoAccess(string connectionString)
        {
            _mongo = new MongoKeyValueDB(connectionString);
        }


        #region IDataAccess 成员

        public DatabaseType DatabaseType
        {
            get { return DatabaseType.MongoDB; }
        }

        public System.Data.IDbConnection DbConnection
        {
            get { return null; }
        }

        public System.Data.IDbTransaction BeginTransaction()
        {
            return null;
        }

        public void Open()
        {
            _mongo.Open();
        }

        public void Close()
        {
            _mongo.Dispose();
            _isClose = true;
        }

        public bool IsClosed
        {
            get { return _isClose; }
        }

        public int ExecuteNonQuery(System.Data.CommandType commandType, string commandText)
        {
            return ExecuteNonQuery(commandText);
        }

        public int ExecuteNonQuery(string commandText)
        {
            if (commandText.StartsWith("insert", StringComparison.CurrentCultureIgnoreCase))
            {
                _convert.ConvertInsert(commandText, _mongo.GetDataBase);
                return 0;
            }
            if (commandText.StartsWith("update", StringComparison.CurrentCultureIgnoreCase))
            {
                _convert.ConvertUpdate(commandText, _mongo.GetDataBase);
                return 0;
            }
            if (commandText.StartsWith("delete", StringComparison.CurrentCultureIgnoreCase))
            {
                _convert.ConvertDelete(commandText, _mongo.GetDataBase);
                return 0;
            }
            throw new Exception("无效命令");
        }

        public int ExecuteNonQuery(string commandText, QueryParameterCollection commandParameters)
        {
            return ExecuteNonQuery(commandText);
        }

        public int ExecuteNonQuery(System.Data.DataRow row, SqlStruct sqlStruct)
        {
            throw new NotImplementedException();
        }

        public int ExecuteNonQuery(System.Data.CommandType commandType, string commandText, QueryParameterCollection commandParameters)
        {
            return ExecuteNonQuery(commandText);
        }

        public int ExecuteNonQuery(System.Data.CommandType commandType, string commandText, QueryParameterCollection commandParameters, System.Data.DataRow row, SqlStruct sqlStruct)
        {
            return ExecuteNonQuery(commandText);
        }

        public System.Data.DataSet ExecuteDataset(System.Data.CommandType commandType, string commandText)
        {
            return ExecuteDataset(commandText);
        }

        public System.Data.DataSet ExecuteDataset(string commandText)
        {
            return _convert.ConvertSelect(commandText, _mongo.GetDataBase) as DataSet;
        }

        public System.Data.DataSet ExecuteDataset(System.Data.CommandType commandType, string commandText, QueryParameterCollection commandParameters)
        {
            return ExecuteDataset(commandText);
        }

        public System.Data.DataSet ExecuteDataset(string commandText, QueryParameterCollection commandParameters)
        {
            return ExecuteDataset(commandText);
        }

        public System.Data.DataSet ExecuteDataset(System.Data.CommandType commandType, string commandText, string tableName)
        {
            return ExecuteDataset(commandText);
        }

        public System.Data.DataSet ExecuteDataset(string commandText, string tableName)
        {
            return ExecuteDataset(commandText);
        }

        public System.Data.DataSet ExecuteDataset(System.Data.CommandType commandType, string commandText, QueryParameterCollection commandParameters, string tableName)
        {
            return ExecuteDataset(commandText);
        }

        public System.Data.DataSet ExecuteDataset(string commandText, QueryParameterCollection commandParameters, string tableName)
        {
            return ExecuteDataset(commandText);
        }

        public System.Data.DataSet ExecuteDataset(System.Data.CommandType commandType, string commandText, System.Data.DataSet ds)
        {
            ds = ExecuteDataset(commandText);
            return ds;
        }

        public System.Data.DataSet ExecuteDataset(string commandText, System.Data.DataSet ds)
        {
            ds = ExecuteDataset(commandText);
            return ds;
        }

        public System.Data.DataSet ExecuteDataset(System.Data.CommandType commandType, string commandText, QueryParameterCollection commandParameters, System.Data.DataSet ds)
        {
            ds = ExecuteDataset(commandText);
            return ds;
        }

        public System.Data.DataSet ExecuteDataset(string commandText, QueryParameterCollection commandParameters, System.Data.DataSet ds)
        {
            ds = ExecuteDataset(commandText);
            return ds;
        }

        public System.Data.DataSet ExecuteDataset(System.Data.CommandType commandType, string commandText, System.Data.DataSet ds, string tableName)
        {
            ds = ExecuteDataset(commandText);
            return ds;
        }

        public System.Data.DataSet ExecuteDataset(string commandText, System.Data.DataSet ds, string tableName)
        {
            ds = ExecuteDataset(commandText);
            return ds;
        }

        public System.Data.DataSet ExecuteDataset(System.Data.CommandType commandType, string commandText, QueryParameterCollection commandParameters, System.Data.DataSet ds, string tableName)
        {
            ds = ExecuteDataset(commandText);
            return ds;
        }

        public System.Data.DataSet ExecuteDataset(string commandText, QueryParameterCollection commandParameters, System.Data.DataSet ds, string tableName)
        {
            ds = ExecuteDataset(commandText);
            return ds;
        }

        public System.Data.DataSet ExecuteDataset(string commandText, QueryParameterCollection commandParameters, System.Data.DataSet ds, int startRecord, int maxRecords, string tableName)
        {
            ds = ExecuteDataset(commandText);
            return ds;
        }

        public System.Data.IDataReader ExecuteReader(System.Data.CommandType commandType, string commandText)
        {
            throw new NotImplementedException();
        }

        public System.Data.IDataReader ExecuteReader(string commandText)
        {
            throw new NotImplementedException();
        }

        public System.Data.IDataReader ExecuteReader(System.Data.CommandType commandType, string commandText, QueryParameterCollection commandParameters)
        {
            throw new NotImplementedException();
        }

        public System.Data.IDataReader ExecuteReader(string commandText, QueryParameterCollection commandParameters)
        {
            throw new NotImplementedException();
        }

        public object ExecuteScalar(System.Data.CommandType commandType, string commandText)
        {
            return ExecuteScalar(commandText);
        }

        public object ExecuteScalar(string commandText)
        {
            return _convert.ConvertSelect(commandText, _mongo.GetDataBase);
        }

        public object ExecuteScalar(System.Data.CommandType commandType, string commandText, QueryParameterCollection commandParameters)
        {
            return ExecuteScalar(commandText);
        }

        public object ExecuteScalar(string commandText, QueryParameterCollection commandParameters)
        {
            throw new NotImplementedException();
        }

        public System.Xml.XmlReader ExecuteXmlReader(System.Data.CommandType commandType, string commandText)
        {
            throw new NotImplementedException();
        }

        public System.Xml.XmlReader ExecuteXmlReader(string commandText)
        {
            throw new NotImplementedException();
        }

        public System.Xml.XmlReader ExecuteXmlReader(System.Data.CommandType commandType, string commandText, QueryParameterCollection commandParameters)
        {
            throw new NotImplementedException();
        }

        public System.Xml.XmlReader ExecuteXmlReader(string commandText, QueryParameterCollection commandParameters)
        {
            throw new NotImplementedException();
        }

        public string Owner
        {
            get;
            set;
        }

        #endregion

        #region IDataStructure 成员

        public void FillingTables(List<TableInfo> info)
        {
            _mongo.GetCollecton(_mongo.DBName).ForEach(o => info.Add(new TableInfo
                                                               {
                                                                   TableName = o
                                                               }));
        }

        public void FillingColumns(List<ColumnInfo> info)
        {
            _mongo.GetCollecton(_mongo.DBName).ForEach(o =>
                                                           {

                                                               try
                                                               {
                                                                   var documentEntity =
                                                                       _mongo.GetDataBase.GetCollection(o).FindOne(null);
                                                                   if (documentEntity == null)
                                                                       return;
                                                                   ICollection<string> keys = documentEntity.Keys;
                                                                   if (keys.Count > 0)
                                                                   {
                                                                       info.AddRange(
                                                                           keys.Select(
                                                                               column =>
                                                                               new ColumnInfo
                                                                                   {
                                                                                       TableName = o,
                                                                                       ColumnName = column
                                                                                   }));
                                                                   }

                                                               }
                                                               catch
                                                               {
                                                               }

                                                           });
        }

        public void FillingIndexs(List<IndexInfo> info)
        {

        }

        public void FillingRelations(List<RelationshipInfo> info)
        {

        }

        public void FillingViews(List<ViewInfo> info)
        {

        }

        public void FillingProcs(List<ProcInfo> info)
        {

        }

        public void FillingProcParams(List<ProcParamInfo> info)
        {

        }
        public void FillingTypeInfos(List<TypeNameInfos> info)
        {
        }
        #endregion
    }
}
